#!/bin/bash

#C compiler cc is not found
apt install gcc g++ make

#the HTTP rewrite module requires the PCRE library
apt install libpcre3 libpcre3-dev

#SSL modules require the OpenSSL library
apt install libssl-dev

#the HTTP image filter module requires the GD library
apt install libgd-dev

#the geoip2 module requires the maxminddb library
apt install libmaxminddb0 libmaxminddb-dev mmdb-bin

echo "PCRE OpenSSL GD maxminddb library installed by apt"


dpkg -l | grep -i zlib > /dev/null 2>&1
if [ $? -eq 1 ]; then
    #the HTTP gzip module requires the zlib library
    wget -nc https://www.zlib.net/zlib-1.3.1.tar.gz
    tar -zxvf zlib-1.3.1.tar.gz
    cd zlib-1.3.1/
    ./configure && make && make install
    cd ../
    echo "zlib library installed by complie"
    read
fi

read -p "install nginx? [y/n]" -n 1 input
if [ $input = "y" ];then
    wget -nc https://tengine.taobao.org/download/tengine-3.1.0.tar.gz
    tar -zxvf tengine-3.1.0.tar.gz

    wget -nc https://nginx.org/download/nginx-1.26.1.tar.gz
    tar -zxvf nginx-1.26.1.tar.gz
    cd nginx-1.26.1/
    ./configure \
        --with-http_ssl_module \
        --with-http_v2_module \
        --with-http_image_filter_module=dynamic \
        --with-http_realip_module \
        --with-http_sub_module \
        --with-http_addition_module \
        --add-module=/root/source/tengine-3.1.0/modules/ngx_slab_stat \
        --add-module=/root/source/tengine-3.1.0/modules/ngx_http_concat_module \
        --add-dynamic-module=/root/source/ngx_http_geoip2_module \
    && make -j8 && make install
    cd ..
    echo "nginx installed by complie"
fi

read -p "install openresty? [y/n]" -n 1 input
if [ $input = "y" ];then
    wget -nc https://openresty.org/download/openresty-1.25.3.1.tar.gz
    tar -zxvf openresty-1.25.3.1.tar.gz
    cd openresty-1.25.3.1
    ./configure \
        --with-http_ssl_module \
        --with-http_v2_module \
        --with-http_image_filter_module=dynamic \
        --with-http_realip_module \
        --with-http_sub_module \
        --with-http_addition_module \
        --add-module=/root/source/tengine-3.1.0/modules/ngx_slab_stat \
        --add-module=/root/source/tengine-3.1.0/modules/ngx_http_concat_module \
        --add-dynamic-module=/root/source/ngx_http_geoip2_module \
    && make -j8 && make install
    cd ..
    echo "openresty installed by complie"
    read
fi